
package edu.gatech.wms.junits;

import android.test.AndroidTestCase;
import edu.gatech.wms.controllers.Login;
import edu.gatech.wms.controllers.UserManager;
import edu.gatech.wms.models.User;

/**
 * 
 * @author Jamie Barnwell
 * Tests the authUser method from the Login.java class which takes in an e-mail and password
 * and compares the given parameters with what is in the database to see if the User exists, 
 * if the account is locked, and if he gave the correct password.
 */
public class AuthUserTest extends AndroidTestCase {
	
	//private static final int LOGIN_LOCKED = -1;
	//private static final int LOGIN_FAILED = 0;
	//private static final int LOGIN_SUCCESS = 1;
	private Login testLogin = Login.getLoginObject();
	
	public void testAuthUser() {
			
		
		//check for non-existent user
		String not = "IDontExist@aol.com";
		int notResult = testLogin.authUser(not,"password");
		
		//check for correct user with wrong password
		String janeMail = "jane@yahoo.com";
		String wrongPass = "notpassword";
		int wrongP = testLogin.authUser(janeMail, wrongPass);

		wrongP = testLogin.authUser(janeMail, wrongPass);
		
		//check that the tryCount for that user has been incremented
		//to 1 for the first failed attempt to login.
		int try1 = 1;

		int tries = UserManager.dbMain.queryUser("jane@yahoo.com").getTryCount();
		//tries = UserManager.dbMain.queryUser("jane@yahoo.com").getTryCount();
		
		
		//Check that password is case-sensitive and that trycount is again incremented
		String passCaps = "PASSWORD";
		int wrongCaps = testLogin.authUser(janeMail, passCaps);
		int try2 = 2;
		int tries2 = UserManager.dbMain.queryUser("jane@yahoo.com").getTryCount();
		
		//check that user's try count is incremented again and that
		//account is locked after 3 unsuccessful tries
		String wrongPass3 = "notHappening";
		int wrong3 = testLogin.authUser(janeMail, wrongPass3);
		int try3 = 3;
		int tries3 = UserManager.dbMain.queryUser("jane@yahoo.com").getTryCount();
		boolean janeLocked = UserManager.dbMain.queryUser("jane@yahoo.com").getLockedStatus();
		
		
		//check that notified that account is locked when locked
		//even if password is correct
		String password = "password";
		int lockedLog = testLogin.authUser(janeMail, password);
		
		
		//check that user logs in if correct mail/password given and
		//that try count resets to 0 if user successfully logs in 
		//before max number of failed attempts.
		String jamesMail = "brown@aol.com";
		int jamesTry = 1;
		int wrongJames = testLogin.authUser(jamesMail, wrongPass);
		int jamesTries = UserManager.dbMain.queryUser("brown@aol.com").getTryCount();
		
		int jamesTryRight = 0;
		int rightJames = testLogin.authUser(jamesMail, password);
		int jamesTriesRight = UserManager.dbMain.queryUser("brown@aol.com").getTryCount();
		//now check that activeKey is set to user's id and that
		//isAdmin is set to 0 for non-admin user.
		int jamesID = UserManager.dbMain.queryUser("brown@aol.com").getId();
		int jamesActKey = testLogin.getActiveKey();
		
		boolean notAdmin = testLogin.getIsAdmin();
		
		
		//check that login is successful when correct email/password
		//combination is given, that the activeKey is reset to user's id,
		//and that isAdmin is set to true for admin users.
		String adminMail = "admin@gatech.edu";
		int correct = testLogin.authUser(adminMail, password);

		int adminID = UserManager.dbMain.queryUser("admin@gatech.com").getId();
		int adminActKey = testLogin.getActiveKey();
		
		boolean yesAdmin = testLogin.getIsAdmin();
		
		
		
		assertEquals(Login.LOGIN_FAIL, notResult); //wrong email
		
		assertEquals(Login.LOGIN_FAIL, wrongP);//wrong password
		assertEquals(try1, tries); //show that login tries was incremented
		
		assertEquals(Login.LOGIN_FAIL, wrongCaps);//wrong case-sensitive
		assertEquals(try2, tries2);//login tries incremented
		
		assertEquals(Login.LOGIN_FAIL, wrong3);//wrong password
		assertEquals(try3, tries3); //login tries incremented to max
		assertEquals(true, janeLocked);//user account locked at max tries
		
		assertEquals(Login.LOGIN_LOCKED, lockedLog); //try to login to locked account
		
		assertEquals(Login.LOGIN_FAIL, wrongJames); //different user, wrong password
		assertEquals(jamesTry, jamesTries); //first unsuccessful try incremented
		
		assertEquals(Login.LOGIN_SUCCESS, rightJames); //successful login
		assertEquals(jamesTryRight, jamesTriesRight); //try count goes back to 0
		
		assertEquals(jamesID, jamesActKey); //activeKey should be logged in user's id
		assertEquals(false, notAdmin); //isAdmin should be false for non-admins
		
		assertEquals(Login.LOGIN_SUCCESS, correct); //admin login successful
		assertEquals(adminID, adminActKey); //activeKey changed to admin's id
		assertEquals(true, yesAdmin); //isAdmin should be true for admin users

	}

}
